package com.tourex.ui.view.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.tourex.App;
import com.tourex.App.MyLocationListener;
import com.tourex.C.bean.Api01;
import com.tourex.C.bean.ImageInfo;
import com.tourex.R;
import com.tourex.ui.WapActivity;

public class MainWeatherLayout extends FrameLayout {

	private ViewPager viewPager;
	private List<ImageView> imageViews = new ArrayList<ImageView>();

	private TextView txLocation;

	private ImageLoader imageLoader;

	private View layWeather;
	private TextView txT;

	public MainWeatherLayout(Context context) {
		this(context, null);
	}

	public MainWeatherLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MainWeatherLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		LayoutInflater.from(getContext()).inflate(R.layout.layout_weather, this);
		viewPager = (ViewPager) findViewById(R.id.viewpager);

		txLocation = (TextView) findViewById(R.id.tx_location);

		layWeather = findViewById(R.id.layout_weather);
		txT = (TextView) findViewById(R.id.tx_t);

		imageLoader = App.i().imageLoader;
	}

	public void setData(Api01 api01) {
		imageViews.clear();
		if (api01.imgList != null) {
			for (final ImageInfo imgInfo : api01.imgList) {
				String imgUrl = imgInfo.img;
				ImageView imageView = new ImageView(getContext());
				imageView.setScaleType(ScaleType.FIT_XY);
				imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				imageView.setTag(imgUrl);
				imageLoader.get(imgUrl, ImageLoader.getImageListener(imageView, R.color.image_load, R.color.image_error));
				imageViews.add(imageView);

				imageView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						WapActivity.actionStart(getContext(), imgInfo.url);
					}
				});
			}
		}
		viewPager.setAdapter(new MyPagerAdapter());

		if (TextUtils.isEmpty(App.i().mli.address)) {
			App.i().registerLocationListener(myLocationListener);
		} else {
			txLocation.setText(String.format(getResources().getString(R.string.location_1), App.i().mli.address));
		}
	}

	private MyLocationListener myLocationListener = new MyLocationListener() {
		@Override
		public void onLocation() {
			txLocation.setText(String.format(getResources().getString(R.string.location_1), App.i().mli.address));
			App.i().unRegisterLocationListener(myLocationListener);
		}
	};

	public void setWeather(String t) {
		layWeather.setVisibility(View.VISIBLE);
		txT.setText(String.format(getResources().getString(R.string.weather_1), t));
	}

	Handler handler = new Handler();

	Runnable autoSliding = new Runnable() {
		@Override
		public void run() {
			int current = viewPager.getCurrentItem();
			int next = current + 1;
			int total = imageViews.size();
			if (next >= total)
				next = 0;
			if (viewPager != null) {
				viewPager.setCurrentItem(next);
				startSliding();
			}
		}
	};

	public void startSliding() {
		handler.removeCallbacks(autoSliding);
		handler.postDelayed(autoSliding, 3000);
	}

	public void stopSliding() {
		handler.removeCallbacks(autoSliding);
	}

	class MyPagerAdapter extends PagerAdapter {

		@Override
		public void destroyItem(View v, int position, Object arg2) {
			((ViewPager) v).removeView(imageViews.get(position));
		}

		@Override
		public int getCount() {
			return imageViews.size();
		}

		@Override
		public Object instantiateItem(View v, int position) {
			((ViewPager) v).addView(imageViews.get(position));
			return imageViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View v, Object arg1) {
			return v == arg1;
		}
	}
}
